package com.tczs.server.request;

import io.netty.handler.codec.http.FullHttpRequest;
import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class DefaultServerBuildRequest extends AbstractServerBuildRequestSwap{
    private static Logger logger = Logger.getLogger(DefaultServerBuildRequest.class);

    /**
     * @param msg  前端传来的request
     * @return
     */
    @Override
    public ServerRequestModel buildRequest(FullHttpRequest msg){
        byte[] buffer = msg.toString().getBytes();
        String str = "";
        try {
            str = URLDecoder.decode(new String(buffer), "UTF-8");
            logger.info(str);
        } catch (UnsupportedEncodingException e) {
            logger.info("URLDecoder decode error",e);
        }
        String[] array = str.split(System.getProperty("line.separator"));
        ServerRequestModel request = new ServerRequestModel();
        request.setUrl(msg.uri());
        request.setMethod(msg.method().name());
        request.setVersion(msg.protocolVersion().text());
        request = getParamFromFirstLine(request);
        request = completeRequest(request,array);
        logger.info(request.toString());
        return request;
    }
}
